﻿@model  Authentication.Model.ViewModel.Account.AssignGroupObjectViewModel
<ul class="breadcrumb">
    <li>
        <i class="icon-home"></i>
        <a href="/">Account</a>
        <i class="icon-angle-right"></i>
    </li>
    <li>
        <i class="icon-link"></i>
        <a href="#">Assign rules to group</a>
    </li>
</ul>
<!--/span-->
@using (Html.BeginForm("assign-group-objects", "account", FormMethod.Post, new { @class = "form-horizontal" }))
{
    <div class="row-fluid">
        <div class="box span12">
            <div class="box-header" data-original-title>
                <h2><i class="halflings-icon lock"></i><span class="break"></span>Assign menu items to a group</h2>
                <div class="box-icon">
                </div>
            </div>
            <div class="box-content">
                <div class="validation-summary alert alert-error">
                    <a class="close" data-dismiss="alert">×</a>
                    <h5 class="alert-heading">Assign validate</h5>
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary()
                </div>

                <div class="box-content">
                    <div class="box-1 span4">
                        <div class="priority high"><span><i class="icon-group"></i>&nbsp;Group user</span></div>
                        <div class="task high">
                            <div class="desc">
                                <div class="title">Select group</div>
                                <div>select group to contain users that will be assigned</div>
                                <div>
                                    @Html.DropDownListFor(m => m.GroupIdentifier, new SelectList(Model.Groups, "GroupIdentifier", "GroupName"), "-- Select group --", new { @class = "row-fluid" })
                                </div>
                            </div>
                        </div>
                        @if (Model.IsSupperAdmin)
                        {
                            <div class="desc">
                                <button id="btnApply" type="submit" class="btn btn-primary"><i class="icon-ok"></i>Apply change</button>
                            </div>
                        }
                    </div>
                    <div class="box-1 span8">
                        <div class="priority low"><span><i class="icon-lock"></i>&nbsp;Menu items list</span></div>
                        <div class="task low">
                            <table class="table table-striped table-custom-2 table-bordered bootstrap-datatable datatable" id="dataTables-1">
                                <thead>
                                    <tr>
                                        <th class="text-left" data-class="expand" data-priority="2"><b>Id</b></th>
                                        <th class="text-left" data-class="expand">Display Name</th>
                                        <th class="text-left" data-class="expand">Link Url</th>
                                        @foreach (var item in Model.Roles)
                                        {
                                            <th class="text-left" data-class="expand">@item.Name</th>
                                        }
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var rowitem in Model.Objects)
                                    {
                                        <tr>
                                            <td class="text-left"><b>@rowitem.ObjectIdentifier</b></td>
                                            <td class="text-left">@rowitem.ObjectName</td>
                                            <td class="text-left">@rowitem.ObjectUrl</td>

                                            @foreach (var roleitem in Model.Roles)
                                            {
                                                var ObjectRoleIdentifier = string.Format("ObjectIdentifier:\"{0}\",RoleIdentifier:\"{1}\"", rowitem.ObjectIdentifier, roleitem.RoleIdentifier);
                                                <td class="center">
                                                    @if (Model.ObjectRole_GroupIdentifier != null && Model.ObjectRole_GroupIdentifier.ContainsKey(ObjectRoleIdentifier))
                                                    {
                                                        <input type='checkbox' data-no-uniform="true" name="group_roles" value='@ObjectRoleIdentifier' group-identifiers="@Model.ObjectRole_GroupIdentifier[ObjectRoleIdentifier]" />
                                                    }
                                                    else
                                                    {
                                                        <input type='checkbox' data-no-uniform="true" name="group_roles" value='@ObjectRoleIdentifier' />
                                                    }
                                                </td>
                                            }
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--/span-->
    </div>
}
<!--/row-->
@section scripts
{
    @if (true)
    {
        <script type="text/javascript">
            $(document).ready(function () {
                ResetCheckboxes();

                $("#GroupIdentifier").change(function () {
                    ResetCheckboxes();
                });

                $("#btnApply").click(function () {
                    if ($('input[type="search"]').val().length > 0) {
                        alert('Please clear search data on grid.');
                        $('input[type="search"]').focus();
                        return false;
                    }
                    return true;
                });
            });

            function ResetCheckboxes() {
                var groupid = $("#GroupIdentifier").val();
                var checkboxes = $('[name="group_roles"]');
                $.each(checkboxes, function (index, item) {
                    if ($(item).attr('group-identifiers') != null) {
                        var res = $(item).attr('group-identifiers').split(';');
                        if ($.inArray(groupid, res) != -1) {
                            $(item).attr('checked', 'checked')
                        }
                        else {
                            $(item).removeAttr('checked');
                        }
                    }
                });
            }
        </script>
    }
}